﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class delete : Window
    {
        private int table;
        private string ID, IDx;
        public delete(int table, string ID)
        {
            this.table = table;
            this.ID = ID;
            InitializeComponent();
        }

        public delete(int table, string ID1, string ID2)
        {
            this.table = table;
            this.ID = ID1;
            this.IDx = ID2;
            InitializeComponent();
        }
        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                homeWindow.Con.Open();

                string delete = "";
                if (table == topWindow.REKLAME)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        delete = @"delete from menyewa where 
                                        ID_Reklame = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();
                        delete = "delete from " + label5.Content.ToString() + @" where 
                                        ID_Reklame = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();
                        MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                    }
                }
                else if (table == topWindow.PENYEWAAN)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        delete = @"delete from menyewa where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        delete = "delete from menggunakan " + @" where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        delete = "delete from rencanapembayaran " + @" where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        SqlDataAdapter da = new SqlDataAdapter("select ID_CP from menghubungi where ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';",homeWindow.Con);
                        DataSet das = new DataSet();
                        da.Fill(das, "Test");

                        delete = "delete from menghubungi " + @" where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        if (das.Tables["Test"].Rows.Count > 0)
                        {
                            delete = "delete from ContactPerson " + @" where 
                                            ID_CP = '" + das.Tables["Test"].Rows[0].ItemArray.ElementAt(0).ToString() + "';";
                            _cmd = new SqlCommand(delete, homeWindow.Con);

                            // Execute the command
                            _cmd.ExecuteNonQuery();
                        }

                        delete = "delete from pembayaran " + @" where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        delete = "delete from penyewaan " + @" where 
                                        ID_Penyewaan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                    }
                }
                else if (table == topWindow.PEMBELIAN)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        SqlDataAdapter da = new SqlDataAdapter("Select Jumlah from Pembelian where ID_Laporan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "' and ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[1].ToString() + "';", homeWindow.Con);
                        DataSet dss = new DataSet();
                        da.Fill(dss, "tes");

                        da = new SqlDataAdapter("Select Total from Daftar_Barang where ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[1].ToString() + "'", homeWindow.Con);
                        DataSet ds = new DataSet();
                        da.Fill(ds, "temp");

                        int jumlah = Int32.Parse(ds.Tables["temp"].Rows[0].ItemArray.ElementAt(0).ToString()) - Int32.Parse(dss.Tables["tes"].Rows[0].ItemArray.ElementAt(0).ToString());

                        delete = @"delete from Pembelian 
                                where ID_Laporan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() +
                                                          "' and ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[1].ToString() + "';";
                        SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);
                        _cmd.ExecuteNonQuery();

                        delete = @"update Daftar_Barang set Total = " + jumlah + " where ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[1].ToString() + "'";
                        _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        // Check whether element where ID_Laporan == ID is exist or not
                        string text = "select * from Pembelian where ID_Laporan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "'";
                        da = new SqlDataAdapter(text, homeWindow.Con);
                        ds = new DataSet();
                        da.Fill(ds, "Laporan Pembelian Spesifik");

                        int count = ds.Tables["Laporan Pembelian Spesifik"].Rows.Count;

                        if (count == 0)
                        {
                            delete = @"delete from Laporan_Barang
                                    where ID_Laporan = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                            _cmd = new SqlCommand(delete, homeWindow.Con);

                            // Execute the command
                            _cmd.ExecuteNonQuery();
                        }
                        MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                    }
                }
                else if (table == topWindow.PEMBAYARAN)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        delete = @"delete from Pembayaran where 
                                        ID_Pembayaran = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                        SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                        // Execute the command
                        _cmd.ExecuteNonQuery();
                        MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                    }
                }
                else if (table == topWindow.PEMAKAIAN)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        string text = "select ID_Barang,Total from Daftar_Barang where Nama_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "'";
                        SqlDataAdapter da = new SqlDataAdapter(text, homeWindow.Con);
                        DataSet ds = new DataSet();
                        da.Fill(ds, "temp");

                        da = new SqlDataAdapter("Select Jumlah,id_pemakaian from Menggunakan where ID_Pemakaian = '" + IDx + "' and ID_Barang = '" + ds.Tables["temp"].Rows[0].ItemArray.ElementAt(0).ToString() + "';", homeWindow.Con);
                        DataSet dss = new DataSet();
                        da.Fill(dss, "tes");

                        delete = @"delete from Menggunakan where  
                               ID_pemakaian = '" + IDx + "';";
                        SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);
                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        int jumlah = Int32.Parse(ds.Tables["temp"].Rows[0].ItemArray.ElementAt(1).ToString()) + Int32.Parse(dss.Tables["tes"].Rows[0].ItemArray.ElementAt(0).ToString());

                        delete = @"update Daftar_Barang set Total = " + jumlah + " where ID_Barang = '" + ds.Tables["temp"].Rows[0].ItemArray.ElementAt(0).ToString() + "'";
                        _cmd = new SqlCommand(delete, homeWindow.Con);
                        // Execute the command
                        _cmd.ExecuteNonQuery();

                        MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                    }
                }
                else if (table == topWindow.GUDANG)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        SqlDataAdapter da = new SqlDataAdapter("select * from Menggunakan where ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';", homeWindow.Con);
                        DataSet ds = new DataSet();
                        da.Fill(ds, "Menggunakan");

                        da = new SqlDataAdapter("select * from Pembelian where ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';", homeWindow.Con);
                        DataSet dss = new DataSet();
                        da.Fill(dss, "Pembelian");

                        if (ds.Tables["Menggunakan"].Rows.Count == 0 && dss.Tables["Pembelian"].Rows.Count == 0)
                        {

                            delete = @"delete from Daftar_Barang where 
                                            ID_Barang = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "';";
                            SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                            // Execute the command
                            _cmd.ExecuteNonQuery();
                            MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                        }
                        else
                        {
                            MessageBox.Show(((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + " masih digunakan pada tabel lain", "Error:External Use");
                        }
                    }
                }
                else if (table == topWindow.PENGGUNA)
                {
                    for (int i = 0; i < homeWindow.top.dr.SelectedItems.Count; i++)
                    {
                        if (!((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString().Equals(topWindow.username) && !((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString().Equals("admin"))
                        {
                            delete = @"delete from laporan_barang where 
                                            username = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "'";
                            SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                            // Execute the command
                            _cmd.ExecuteNonQuery();

                            delete = @"delete from pengguna where 
                                            username = '" + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString() + "'";
                            _cmd = new SqlCommand(delete, homeWindow.Con);

                            // Execute the command
                            _cmd.ExecuteNonQuery();
                            MessageBox.Show("Berhasil menghapus " + ((DataRowView)(homeWindow.top.dr.SelectedItems[i])).Row[0].ToString(), "Delete");
                        }
                        else
                        {
                            MessageBox.Show("Tidak bisa menghapus akun yang sedang digunakan", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                else if (table == topWindow.CP)
                {
                    delete = @"delete from Menghubungi where 
                                    ID_CP = '" + ID + "'";
                    SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                    // Execute the command
                    _cmd.ExecuteNonQuery();

                    delete = @"delete from ContactPerson where 
                                    ID_CP = '" + ID + "'";
                    _cmd = new SqlCommand(delete, homeWindow.Con);

                    // Execute the command
                    _cmd.ExecuteNonQuery();
                }
                else if (table == topWindow.RENCANAPEMBAYARAN)
                {
                    delete = @"delete from RencanaPembayaran where
                                ID_RP = '" + ID + "'";
                    SqlCommand _cmd = new SqlCommand(delete, homeWindow.Con);

                    // Execute the command
                    _cmd.ExecuteNonQuery();
                }

                homeWindow.Con.Close();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
            if (table != topWindow.CP && table != topWindow.RENCANAPEMBAYARAN)
            {
                homeWindow.top.refresh();
                homeWindow.top.Focus();
            }
            else if (table == topWindow.CP)
            {
                topWindow.contactperson.FillData();
                topWindow.contactperson.Focus();
            }
            else if (table == topWindow.RENCANAPEMBAYARAN)
            {
                topWindow.rencpemb.FillData();
                topWindow.rencpemb.Focus();
            }
            this.Close();
        }
        
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (table == topWindow.REKLAME)
            {
                label5.Content = "Reklame";
                label4.Content = ID;
            }
            else if (table == topWindow.PENYEWAAN)
            {
                label5.Content = "Penyewaan";
                label4.Content = ID;
            }
            else if (table == topWindow.PEMBELIAN)
            {
                label5.Content = "Pembelian";
                label4.Content = ID;
            }
            else if (table == topWindow.PEMBAYARAN)
            {
                label5.Content = "Pembayaran";
                label4.Content = ID;
            }
            else if (table == topWindow.PEMAKAIAN)
            {
                label5.Content = "Pemakaian";
                label4.Content = ID;
            }
            else if (table == topWindow.GUDANG)
            {
                label5.Content = "Gudang";
                label4.Content = ID;
            }
            else if (table == topWindow.PENGGUNA)
            {
                label5.Content = "Pengguna";
                label4.Content = ID;
            }
            else if (table == topWindow.CP)
            {
                label5.Content = "Contact Person";
                label4.Content = IDx;
            }
            else if (table == topWindow.RENCANAPEMBAYARAN)
            {
                label5.Content = "Rencana Pembayaran";
                label4.Content = ID;
            }

            if (homeWindow.top.dr.SelectedItems.Count > 1)
            {
                label4.Content = homeWindow.top.dr.SelectedItems.Count + " items";
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (table != topWindow.CP && table != topWindow.RENCANAPEMBAYARAN)
            {
                homeWindow.top.Focus();
            }
        }
    }
}
